package com.atguigu.lease.web.admin.controller.apartment;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "标签管理")
@RestController
@RequestMapping("/admin/label")
public class LabelController {
  @Autowired private LabelInfoService service;

  @Operation(summary = "（根据类型）查询标签列表")
  @GetMapping("list")
  public Result<List<LabelInfo>> labelList(@RequestParam(required = false) ItemType type) {
    // 前端传来的字符串"1" 被转化成对应的枚举类ItemType.APARTMENT
    // 查询条件
    List<LabelInfo> list =
        service.list(
            new LambdaQueryWrapper<LabelInfo>().eq(type != null, LabelInfo::getType, type));
    return Result.ok(list);
  }

  @Operation(summary = "新增或修改标签信息")
  @PostMapping("saveOrUpdate")
  public Result saveOrUpdateLabel(@RequestBody LabelInfo labelInfo) {
    return Result.build(service.saveOrUpdateLabel(labelInfo));
  }

  @Operation(summary = "根据id删除标签信息")
  @DeleteMapping("deleteById")
  public Result deleteLabelById(@RequestParam Long id) {
    return Result.build(service.removeById(id));
  }
}
